<?php

namespace app\api\validate;

use app\common\lib\Str;
use think\Validate;

class User extends Validate
{
    protected $rule = [
        'js_code' => 'require',
        'phone_number' => 'require|checkPhone',
        'avatar' => 'require',
        'id_card' => 'require|checkIdCard',
        'sex' => ['require', "in" => "0,1,2"],
    ];

    protected $message = [
        'js_code' => '登录凭证code必须',
        'phone_number' => '手机号必须',
        'avatar' => '头像必须',
        'id_card' => '身份证号必须',
        'sex.require' => '性别必须',
        'sex.in' => '性别数值错误',
    ];

    //!/^1[3456789]\d{9}$/
    protected function checkPhone($value, $rule, $data = [])
    {
        $preg = "/^(1\d{10})$/i";
        if (!preg_match($preg,$value)) {
            return "手机号格式错误";
        }
        return true;
    }

    protected function checkIdCard($value, $rule, $data = [])
    {
        if (!Str::idcardCheck($value)) {
            return "身份证号无效";
        }
        return true;
    }

    protected $scene = [
        'wechat_login' => ['js_code'],
        'wechat_update_phone' => ['phone_number'],
        'update_avatar'=> ['avatar'],
        'update'=> ['name','id_card'],
    ];
}